package com.example.newsapp.ui.screens.otherScreens

import android.annotation.SuppressLint
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.runtime.Composable
import androidx.compose.runtime.CompositionLocalProvider
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.platform.LocalContext
import com.example.newsapp.data.compositionLocal.LocalFocusRequest
import com.example.newsapp.data.database.NewsItems
import com.example.newsapp.data.database.databaseModel.NewsDatabaseModel
import com.example.newsapp.data.list.newsNumsList
import com.example.newsapp.data.list.tempNumsList1
import com.example.newsapp.data.list.tempNumsList2
import com.example.newsapp.data.`object`.StateSave
import com.example.newsapp.ui.components.otherComponent.searchComponent.GuessSearchList
import com.example.newsapp.ui.components.otherComponent.searchComponent.HotSearchList
import com.example.newsapp.ui.components.otherComponent.searchComponent.SearchComponent
import kotlinx.coroutines.runBlocking

// 搜索时的页面
@SuppressLint("CoroutineCreationDuringComposition")
@Composable
fun SearchScreen(modifier: Modifier = Modifier) {
    val focusRequster = remember { FocusRequester() }
    val context = LocalContext.current
    val databaseModel = NewsDatabaseModel(context)
    val scope = rememberCoroutineScope()
    val hotNumsList = mutableListOf<NewsItems>()
    var guessNumsList = mutableListOf<NewsItems>()
    var allNewsNums = 0
    StateSave.gesturesEnable = false

    // 获取所有新闻的数量
    runBlocking {
        allNewsNums = databaseModel.dateSelect.selectAllNewsNums()
    }
    // 只有初始化时，将这个列表打乱
    if (StateSave.hotInitState == 0) {
        // 将所有新闻的数量存到列表里
        for (i in 1..allNewsNums) {
            newsNumsList.add(i)
        }
        newsNumsList.shuffle()
        tempNumsList1.addAll(newsNumsList.take(10))
        tempNumsList2.clear()
        tempNumsList2.addAll(newsNumsList.take(10))
    }
    StateSave.hotInitState++
    // 将打乱后的列表前10个新闻存到热门新闻数列表里
    repeat(10) {
        runBlocking {
            hotNumsList.add(databaseModel.dateSelect.selectByNum(tempNumsList1.get(it)))
        }
    }

    if (!StateSave.searchRefresh) {
        tempNumsList2.clear()
        tempNumsList2.addAll(newsNumsList.shuffled().take(10))
    }

    // 将打乱后的列表前10个新闻存到猜你想搜新闻数列表里
    repeat(10) {
        runBlocking {
            guessNumsList.add(databaseModel.dateSelect.selectByNum(tempNumsList2.get(it)))
        }
    }

    Column(
        modifier = Modifier.fillMaxSize()
    ) {
        CompositionLocalProvider(LocalFocusRequest provides focusRequster) {
            // 顶部搜索栏
            SearchComponent()
            Column(modifier = Modifier) {
                // 猜你想搜列表
                GuessSearchList(guessNumsList = guessNumsList)
                // 热门搜索列表
                HotSearchList(hotNumsList = hotNumsList)
            }
        }
    }
}
